package org.jeecg.modules.maps.service.impl;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.core.constant.YesOrNotEnum;
import org.jeecg.modules.maps.entity.BimMapsInfo;
import org.jeecg.modules.maps.mapper.BimMapsInfoMapper;
import org.jeecg.modules.maps.model.vo.MapsImgVo;
import org.jeecg.modules.maps.service.IBimMapsInfoService;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class BimMapsInfoServiceImpl extends ServiceImpl<BimMapsInfoMapper, BimMapsInfo> implements IBimMapsInfoService {
    @Override
    public void addMapsInfo(BimMapsInfo bimMapsInfo) {
        if (bimMapsInfo.getEnableFlag().equals(YesOrNotEnum.Y.getCode())) {
            List<Integer> ids = this.findByCondition(bimMapsInfo.getMapsParam(), bimMapsInfo.getMapsId());
            if (CollUtil.isNotEmpty(ids)) {
                //禁用同楼栋同楼层的贴图方式
                this.disOrEnable(ids, YesOrNotEnum.N.getCode());
            }
        }
        MapsImgVo mapsImgVo = JSON.parseObject(bimMapsInfo.getMapsParam(), MapsImgVo.class);
        if (mapsImgVo != null) {
            bimMapsInfo.setMapsBuilding(mapsImgVo.getMapsBuilding());
            bimMapsInfo.setMapsFloor(mapsImgVo.getMapsFloor());
            bimMapsInfo.setImgUrl(mapsImgVo.getImgUrl());
        }
        this.save(bimMapsInfo);
    }

    @Override
    public List<MapsImgVo> MapsImgList(Integer mapsId) {
        return baseMapper.MapsImgList(mapsId);
    }

    @Override
    public List<BimMapsInfo> mapsInfoList(Integer mapsId) {
        return new LambdaQueryChainWrapper<>(baseMapper)
                .eq(BimMapsInfo::getMapsId, mapsId)
                .list();
    }


    /**
     * 根据条件查询贴图方式
     *
     * @return
     */
    private List<Integer> findByCondition(String json, Integer mapsId) {
        if (StrUtil.isNotBlank(json)) {
            JSONObject jsonObject = JSON.parseObject(json);
            String projectionBuild = "mapsBuilding";
            String projectionFloor = "mapsFloor";
            Map<String, Object> map = new HashMap<>();
            String building = jsonObject.getString(projectionBuild);
            String floor = jsonObject.getString(projectionFloor);
            if (building != null) {
                map.put(projectionBuild, building);
            }
            if (floor != null) {
                map.put(projectionFloor, floor);
            }
            StringBuilder stringBuilder = new StringBuilder();
            map.forEach((key, val) -> stringBuilder.append(" AND ")
                    .append("maps_param->'$.")
                    .append(key)
                    .append("'=")
                    .append("'")
                    .append(val)
                    .append("'"));
            List<BimMapsInfo> list = new LambdaQueryChainWrapper<>(baseMapper)
                    .select(BimMapsInfo::getId)
                    .eq(BimMapsInfo::getMapsId, mapsId)
                    .last(stringBuilder.toString())
                    .list();
            if (CollUtil.isNotEmpty(list)) {
                return list.stream().map(BimMapsInfo::getId).collect(Collectors.toList());
            }
        }
        return Collections.emptyList();
    }


    private void disOrEnable(List<Integer> ids, Integer enableStatus) {
        new LambdaUpdateChainWrapper<>(baseMapper)
                .set(BimMapsInfo::getEnableFlag, enableStatus)
                .in(BimMapsInfo::getId, ids)
                .update();
    }
}
